<template>
<div class="ivu-card ivu-card-bordered ivu-card-dis-hover">
  <div class="ivu-card-head"><p>{{ $route.meta.title }}</p></div>

  <div class="ivu-card-body">
    <iForm ref="formRef" :model="formFields" :rules="rules" :label-width="100">

      <FormItem prop="type" label="属性：">
        <i-select v-model="formFields.type" placeholder="请选择属性">
          <i-option v-for="(v, k) in apiConfig['user_wallet_type']" :value="k.toString()" :key="k">{{ v }}</i-option>
        </i-select>
      </FormItem>

      <FormItem prop="account" label="账号：">
        <Input v-model="formFields.account" placeholder="账号" @on-change="onChangeFindAccount"/>
      </FormItem>

      <FormItem prop="amount" label="数量：">
        <Input v-model="formFields.amount" placeholder="数量"/>
      </FormItem>

      <FormItem>
        <Button type="primary" :loading="submitLoading" @click="onSubmit">创建</Button>
      </FormItem>
    </iForm>
  </div>
</div>
</template>

<script>
import qs from 'qs';
import { Form, Input, Button, Select, Option, Message } from 'iview';
import ajaxiView from '@/utils/ajaxiView';
import apiConfig from '@/utils/apiConfig';
import auth from '@/utils/auth';

export default {
  components: {
    [Form.name]: Form,
    [Form.Item.name]: Form.Item,
    [Input.name]: Input,
    [Button.name]: Button,
    [Button.Group.name]: Button.Group,
    [Select.name]: Select,
    [Option.name]: Option
  },
  data() {
    return {
      apiConfig: {},
      token: auth.getToken(),
      submitLoading: false,
      formFields: {
        account: '',
        amount: '',
        type: 1
      },
      rules: {
        type: [
          {type:'string', required: true, message: '请选择属性!', trigger: 'blur'}
        ],
        account: [
          {type:'string', required: true, message: '请输入账号!', trigger: 'blur'},
          {type:'string', min: 6, message: '请输入不少于6位的账号!', trigger: 'blur'}
        ],
        amount: [
          {type:'string', required: true, message: '请输入数量!', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    _initialize() {
      var _this = this;
      apiConfig.getItem().then(function(api_config) {
        _this.apiConfig = api_config;
        _this.getData();
      });
    },
    getData() {
      this.viewLoading = true;
    },
    onChangeFindAccount() {
      if(this.formFields.account.replace(/[\u0391-\uFFE5]/g, 'aa').length < 6) {
        return false;
      }

      if(this.formFields.account !== null && this.formFields.account !== undefined && this.formFields.account !== '') {
        ajaxiView.get('/user/account/'+this.formFields.account+'?token='+this.token);
      }
    },
    onSubmit() {
      var _this = this;
      _this.$refs.formRef.validate((valid) => {
        _this.submitLoading = true;
        if(valid) {
          _this.formFields.amount = parseFloat(_this.formFields.amount);
          if(_this.formFields.amount < 0) {
            _this.formFields.active = 2;
          } else if(_this.formFields.amount > 0) {
            _this.formFields.active = 1;
          } else {
            Message.error('数量不能等于0');
            _this.submitLoading = false;
            return false;
          }

          ajaxiView.post('/user/wallet/recharge?token='+this.token, qs.stringify(_this.formFields)).then(function(data) {
            if(data !== false) {
              Message.success({
                content: data.msg,
                onClose: function() {
                  _this.formFields.amount = '';
                  //_this.$router.push({ name: 'userRecharge' });
                  //window.location.reload();
                }
              });
            }

            _this.submitLoading = false;
          });
        } else {
          _this.submitLoading = false;
        }
      });
    }
  },
  mounted() {
    this._initialize();
  }
}
</script>
